<template>
  <div class="app-container">
    <el-card>
      <div style="margin-top: 15px">
      <el-form :inline="true" :model="listQuery" size="small">
        <el-form-item label="资源名称：">
          <el-input
            v-model="listQuery.keyword"
            class="input-width"
            placeholder="资源名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-refresh"
            style=""
            @click="handleResetSearch()"
            size="small"
            >重置</el-button
          >
          <el-button
            icon="el-icon-search"
            type="primary"
            @click="handleSearchList()"
            size="small"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-button
      size="small"
      type="primary"
      icon="el-icon-plus"
      class="btn-add"
      @click="handleAdd()"
      >添加渠道分类</el-button
    >
    <div class="table-container">
      <el-table
        ref="resourceCategoryTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="编号" width="100" align="center" prop="id">
        </el-table-column>
        <el-table-column label="渠道分类名称" align="center" prop="name">
        </el-table-column>
        <el-table-column label="排序" align="center" sortable prop="sort">
        </el-table-column>
        <el-table-column align="center" label="状态" prop="enableStatus">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enableStatus"
              :active-value="1"
              :inactive-value="0"
              @change="changeStatus(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              :disabled="scope.row.categoryId == 1"
              @click="handleUpdate(scope.$index, scope.row)"
              >编辑
            </el-button>
            <el-button
              size="small"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    </el-card>
    <el-dialog
      :title="isEdit ? '编辑分类' : '添加分类'"
      :visible.sync="dialogVisible"
      width="40%"
    >
      <el-form
        :model="resourceCategory"
        ref="resourceCategoryForm"
        label-width="150px"
        size="small"
      >
        <el-form-item label="名称：">
          <el-input
            v-model="resourceCategory.name"
            style="width: 250px"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input
            v-model="resourceCategory.sort"
            style="width: 250px"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="handleDialogConfirm()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  cate,
  haibaocate,
  catelist,
  cateenableStatus,
  deletecate,
} from "@/api/article";
import { formatDate } from "@/utils/date";
const defaultResourceCategory = {
  name: null,
  sort: 0,
};
export default {
  name: "resourceCategoryList",
  data() {
    return {
      isEdit: false,
      list: null,
      listLoading: false,
      dialogVisible: false,
      isEdit: false,
      resourceCategory: Object.assign({}, defaultResourceCategory),
      listQuery: {
        keyword: null,
      },
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery.keyword = "";
      this.getList();
    },
    handleSearchList() {
      this.getList();
    },
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if (rowIndex % 2 === 0) {
        if (columnIndex === 0) {
          return [1, 2];
        } else if (columnIndex === 1) {
          return [0, 0];
        }
      }
    },
    //  添加资源类
    handleAdd() {
      this.dialogVisible = true;
      this.isEdit = false;
      this.resourceCategory = Object.assign({}, defaultResourceCategory);
    },
    // 修改状态
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.isEdit = true;
      this.resourceCategory = Object.assign({}, row);
    },
    //删除分类
    handleDelete(index, row) {
      this.$confirm("是否要删除该分类?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deletecate(row.id).then((response) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        });
      });
    },
    // 修改确认
    handleDialogConfirm() {
      this.$confirm("是否要确认?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (this.isEdit) {
          var obj = {
            id: this.resourceCategory.id,
            name: this.resourceCategory.name,
            sort: this.resourceCategory.sort,
          };
          haibaocate(obj).then((response) => {
            this.$message({
              message: "修改成功！",
              type: "success",
            });
            this.dialogVisible = false;
            this.getList();
          });
        } else {
          cate(this.resourceCategory).then((response) => {
            this.$message({
              message: "添加成功！",
              type: "success",
            });
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
    changeStatus(v) {
      cateenableStatus({
        id: v.id,
        enableStatus: v.enableStatus,
      }).then((res) => {
        this.$message.success("操作成功");
        this.getList();
      });
    },
    // 获取分类列表
    getList() {
      this.listLoading = true;
      catelist(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data;
      });
    },
  },
};
</script>
<style></style>
